package com.lmaos.basetools.sText;

import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.net.MalformedURLException;
import java.util.List;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

import com.lmaos.basetools.base.SBaseUtils;
import com.lmaos.basetools.base.SParamMap;

public class HtmlUtils {

	public static String appendBase(String html,String href, boolean create){
		Document doc = Jsoup.parse(html);
		return appendBase(doc, href, create);
	}
	public static String appendBase(Document doc,String href,boolean create){
		if(TextUtils.isBlank(href)){
			return null;
		}
		Element head = doc.head();
		Elements bases = head.getElementsByTag("base");
		Element base = null;
		if(bases==null||bases.size()==0){
			if(create){
				base = head.appendElement("base");
				base.attr("href", href);
			}
		}else{
			base = bases.get(0);
			if(href.equals(base.attr("href"))){
				return null;
			}
			base.attr("href", href);
		}
		
		return doc.toString();
	}
	
	/**
	 * 说明：追加base		<br>
	 * 时间：2017年7月5日 下午12:51:49
	 *
	 * @param file
	 * @param href
	 * @param create base 不存在是否创建
	 * @param charsetName
	 */
	public static boolean appendBase(File file,String href, boolean create, String charsetName){
		if(!file.canRead()){
			return false;
		}
		try {
			Document doc = Jsoup.parse(file, charsetName);
			String html = appendBase(doc, href, create);
			if(html!=null){
				FileUtils.write(file, html, charsetName);
				return true;
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return false;
	}
	/**
	 * 说明：监听文件的变化		<br>
	 * 时间：2017年7月5日 下午2:01:31
	 *
	 * @param path
	 * @param href
	 * @param create
	 * @param charsetName
	 * @param listenerMs
	 * @return
	 */
	public static int appendBaseByPathAll_listener(String path,String href,boolean create,String charsetName,long listenerMs){
		if(!TextUtils.isBlank(href)){
			
			return appendBaseByPathAll_listener(path, href, create, charsetName,listenerMs, ".html",".htm");
		}
		return 0;
	}
	public static int appendBaseByPathAll_listener(String path,final String href,final boolean create,final String charsetName,final long listenerMs,String... suffixs){
		final List<File> files = FileUtils.findFileBySuffix(path, suffixs);
		int res = appendBaseByPathAll(files, href, create, charsetName);
		new Thread(){
			public void run() {
				try {
					while(true){
						appendBaseByPathAll(files, href, create, charsetName);
						sleep(listenerMs);
					}
				} catch (Exception e) {
					e.printStackTrace();
				}
			};
		}.start();
		return res;
	}
	public static int appendBaseByPathAll(String path,String href,boolean create,String charsetName){
		if(!TextUtils.isBlank(href)){
			return appendBaseByPathAll(path, href, create, charsetName, ".html",".htm");
		}
		return 0;
	}
	public static int appendBaseByPathAll(String path,String href,boolean create,String charsetName,String... suffixs){
		List<File> files = FileUtils.findFileBySuffix(path, suffixs);
		return appendBaseByPathAll(files, href, create, charsetName);
	}
	public static SParamMap uptimes = new SParamMap();
	public static int appendBaseByPathAll(List<File> files,String href,boolean create,String charsetName){
		int index = 0;
		for (File file : files) {
			String path = file.getAbsolutePath();
			long uptime = uptimes.getLong(path);
			if(uptime == 0||uptime!=file.lastModified()){
				if(appendBase(file, href, create, charsetName)){
					index++;
				}
				uptimes.put(path, file.lastModified());
			}
		}
		return index;
	}
	public static void setHtmlById(File file,String id,String html, String charsetName){
		try {
			Document doc = Jsoup.parse(file, charsetName);
			String result = setHtmlById(doc, id, html);
			FileUtils.write(file, result, charsetName);
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	public static String setHtmlById(InputStream in,String id,String html, String charsetName, String baseUri){
		try {
			Document doc = Jsoup.parse(in, charsetName, baseUri);
			return setHtmlById(doc, id, html);
		} catch (IOException e) {
			e.printStackTrace();
		}finally{
			SBaseUtils.close(in);
		}
		return null;
	}
	public static String setHtmlById(Document doc,String id,String html){
		Element element = doc.getElementById(id);
		if(element!=null){
			element.html(html);
		}
		return doc.toString();
	}
	
	public static void main(String[] args) throws MalformedURLException, IOException {
		System.out.println(appendBaseByPathAll_listener("/Users/zhangxingyu/project/ex/html", "http://www.baidu.com", true, "utf-8",5000));
//		System.out.println(appendBase("<!DOCTYPE html><html><head></head></html>", "123456789876543",true));
	}
}
